//
//  SearchDisPlayerController.m
//  highlight
//
//  Created by xiayong on 16/3/11.
//  Copyright © 2016年 xiayong. All rights reserved.
//

#import "SearchDisPlayerController.h"
#import "ZYTokenManager.h"
#import "SearchCell.h"
#define fontCOLOR [UIColor colorWithRed:163/255.0f green:163/255.0f blue:163/255.0f alpha:1]

@interface SearchDisPlayerController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)IBOutlet UITableView * myTableView;
@property (nonatomic,strong)IBOutlet UIView * navView;
@property (nonatomic,strong)IBOutlet UITextField * searchText;

@property (nonatomic,strong)NSMutableArray * searchHistory;
@property (nonatomic,strong)NSArray *myArray;//搜索记录的数组

@end

@implementation SearchDisPlayerController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:NO];
    [self readNSUserDefaults];
    self.navigationController.navigationBarHidden=YES;
}
-(void)viewDidLoad{
      self.automaticallyAdjustsScrollViewInsets = NO;
    _searchHistory = [NSMutableArray array];
    
   // self.view.backgroundColor = [UIColor whiteColor];
    self.myTableView.backgroundColor = BackGray;
    self.searchText.returnKeyType = UIReturnKeySearch;//更改键盘的return
    self.searchText.delegate = self;
    self.myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
    backView.backgroundColor= BlackColor;
    self.myTableView.tableFooterView = backView;
    UIView *keyBoardTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 33)];
    keyBoardTopView.backgroundColor = [UIColor whiteColor];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(keyBoardTopView.bounds.size.width - 60 - 12, 4, 60, 22)];
    [btn setTitle:@"关闭" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onKeyBoardDown) forControlEvents:UIControlEventTouchUpInside];
    [keyBoardTopView addSubview:btn];
      [btn setTitleColor:PinkColor forState:UIControlStateNormal];
    _searchText.inputAccessoryView = keyBoardTopView;
    
}
-(void)onKeyBoardDown{
       [_searchText resignFirstResponder];
    //[self.view endEditing:YES];
}
-(IBAction)cancelBtn:(id)sender{
   // [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)emptyBtn{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清除历史记录" message:@"" preferredStyle: UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
        [ZYTokenManager removeAllArray];
        _myArray = nil;
        [self.myTableView reloadData];
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
-(IBAction)search{
    if (_searchText.text.length > 0) {
        [ZYTokenManager SearchText:_searchText.text];//缓存搜索记录
        [self readNSUserDefaults];
        if ([_delegete respondsToSelector:@selector(searchWithTitle:)]) {
            [_delegete searchWithTitle:_searchText.text];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        [[[UIAlertView alloc] initWithTitle: @"请输入查找内容" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
        return;
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{//搜索方法
    
    if (textField.text.length > 0) {
        [ZYTokenManager SearchText:textField.text];//缓存搜索记录
        [self readNSUserDefaults];
          [_searchText resignFirstResponder];
        if ([_delegete respondsToSelector:@selector(searchWithTitle:)]) {
            [_delegete searchWithTitle:textField.text];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }else{
        [[[UIAlertView alloc] initWithTitle: @"请输入查找内容" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show]; 
    }
    return YES;
}
- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (_myArray.count>0) {
            return _myArray.count+1;
        }else{
            return 1;
        }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        if(indexPath.row ==0){
            SearchCell *cell = [SearchCell createWithTableView:tableView];
            return cell;
        }else{
            SearchCell * cell = [SearchCell createWithTableView:tableView];
            NSArray* reversedArray = [[_myArray reverseObjectEnumerator] allObjects];
            cell.image.hidden=YES;
            cell.title.text = reversedArray[indexPath.row-1];
            cell.title.textColor = fontCOLOR;
            cell.title.font = [UIFont systemFontOfSize:13];
            return cell;
        }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    SearchCell *cell = (SearchCell*)[self tableView:self.myTableView cellForRowAtIndexPath:indexPath];
    _searchText.text =cell.title.text;
}


-(void)readNSUserDefaults{//取出缓存的数据
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    //读取数组NSArray类型的数据
    NSArray * myArray = [userDefaultes arrayForKey:@"myArray"];
    self.myArray = myArray;
    [self.myTableView reloadData];
    NSLog(@"myArray======%@",myArray);
}


//点击空白隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //[self.view endEditing:YES];
      [_searchText resignFirstResponder];
}

//-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//    [UIView animateWithDuration:0.3 animations:^{
//        CGRect frame=self.view.frame;
//        frame.origin.y=64;
//        self.view.frame=frame;
//    }];
//    
//    return YES;
//}

@end
